import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * The type Add recipe dialog.
 *
 * @author Qiyin Huang
 * @date 24/06/10
 */
public class AddRecipeDialog extends JFrame implements ActionListener {
    private JLabel askName;
    private JTextField foodNameText;
    private JLabel askAmount;
    private JTextField foodAmountText;
    private JLabel askUnit;
    private JComboBox<String> chooseUnit;
    private JButton addButton;
    private JButton endButton;
    private JTextArea displayArea;

    /**
     * Instantiates a new Add recipe dialog.
     */
    public AddRecipeDialog() {
        super("Add the ingredients needed for your recipe");
        setSize(800, 400);
        initElement();
        getIngredientInfo();
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * Init element.
     */
    public void initElement() {
        askName = new JLabel("Food Name: ");
        foodNameText = new JTextField(10);
        askAmount = new JLabel("Amount: ");
        foodAmountText = new JTextField(10);
        askUnit = new JLabel("Unit: ");
        chooseUnit = new JComboBox<>(new String[]{"pcs", "g", "ml"});
        addButton = new JButton(" add ");
        endButton = new JButton("end");
        displayArea = new JTextArea(10, 30);
    }

    /**
     * Gets food info.
     */
    public void getIngredientInfo() {
        Container content = getContentPane();
        content.setLayout(new BorderLayout());

        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());

        panel.add(askName);
        panel.add(foodNameText);

        panel.add(askAmount);
        panel.add(foodAmountText);

        panel.add(askUnit);
        panel.add(chooseUnit);

        panel.add(addButton);
        panel.add(endButton);

        content.add(panel, "North");
        addButton.addActionListener(this);
        endButton.addActionListener(this);
        content.add(new JScrollPane(displayArea), BorderLayout.CENTER);
        displayArea.setCaretPosition(0);

        content.add(displayArea, "Center");
        displayArea.setEditable(false);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == addButton) {
            handleAddAction();
        } else if (e.getSource() == endButton) {
            this.setVisible(false);
            new AddRecipeFrame();
        }
    }

    private void handleAddAction() {
        boolean amountValid = false;
        String result = null;

        double amount = 0;
        try {
            amount = Double.parseDouble(foodAmountText.getText());
            if (amount < 0) {
                result = "Amount must be a non-negative number";
            } else {
                amountValid = true;
            }
        } catch (NumberFormatException numberFormatException) {
            amountValid = false;
            result = "Amount must be a number";
        } finally {
            if (amountValid) {
                AddRecipeFrame.newRecipe.addIngredient(foodNameText.getText(), amount, chooseUnit.getSelectedItem().toString());
                result = "You add " + foodAmountText.getText() + " "
                        + chooseUnit.getSelectedItem().toString() + " "
                        + foodNameText.getText();
            }
            displayArea.append(result + "\n");

            foodNameText.setText("");
            foodAmountText.setText("");
            chooseUnit.setSelectedIndex(0);
        }
    }

}
